//
//  WALoaderView.m
//  WeatherApp
//
//  Created by Yuliya Grasevych on 02.09.14.
//  Copyright (c) 2014 Yuliya Grasevych. All rights reserved.
//

#import "WALoaderView.h"
#import "NSView+FillWithView.h"

#define LOADER_VIEW_BACKGROUND_COLOR [NSColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.2]

@interface WALoaderView ()

@property (strong) NSProgressIndicator *loaderIndicator;

@end

@implementation WALoaderView

-(id)init{
    self = [super init];
    if (self) {
        
        [self setBackgroundColor:LOADER_VIEW_BACKGROUND_COLOR];
        
        // create circular progress indicator
        _loaderIndicator = [NSProgressIndicator new];
        _loaderIndicator.style = NSProgressIndicatorSpinningStyle;
        
        [_loaderIndicator setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:_loaderIndicator];
        // center loaderIndicator
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_loaderIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_loaderIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    }
    return self;
}
- (void)mouseDown:(NSEvent *)theEvent
{
    // empty to prevent click-through to superview
}
- (void)rightMouseDown:(NSEvent *)theEvent
{
    // empty to prevent click-through to superview
}

- (void)scrollWheel:(NSEvent *)theEvent
{
    // empty to prevent click-through to superview
}
- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

+(WALoaderView *)showLoaderOnView:(NSView *)parentView
{
    WALoaderView *loaderView;
    // look for loader on parentView
    // if find one - do not present new
    NSUInteger loaderIndex = [parentView.subviews indexOfObjectPassingTest:^BOOL(NSView *subview, NSUInteger idx, BOOL *stop) {
        
        if ([subview isKindOfClass:[WALoaderView class]]) {
            
            *stop = YES;
            return YES;
        }
        return NO;
    }];
    // if no loader view on parentView - present and start animation
    if (loaderIndex == NSNotFound) {
        
        // create loaderView with parent view frame
        loaderView = [[WALoaderView alloc] init];
        loaderView.frame = parentView.bounds;
        
        [parentView fillWithView:loaderView];
        [loaderView setWantsLayer:YES];
        
        [loaderView.loaderIndicator startAnimation:nil];
    }
    else {
        // return presented loaderView
        loaderView = parentView.subviews[loaderIndex];
    }
    
    return loaderView;
}

+(void)removeLoaderFromView:(NSView *)parentView
{
    // look for loaderView on parent and remove it
    [parentView.subviews enumerateObjectsUsingBlock:^(NSView *obj, NSUInteger idx, BOOL *stop) {
        
        if ([obj isKindOfClass:[WALoaderView class]]) {
            
            [obj removeFromSuperview];
        }
    }];
}

@end
